package com.kc;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.kc.geniform.Geniform;

import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreePrint;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class KnowledgeCrawlerClient extends javax.swing.JFrame {
	private JPanel jPanel1;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JTree jGeniformTree;
	private JScrollPane jScrollPane5;
	private JEditorPane jEditorPane2;
	private JTree jTree1;
	private JScrollPane jScrollPane4;
	private JTabbedPane jTabbedPane2;
	private JScrollPane jScrollPane3;
	private JButton jTransformButton;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JPanel jGeniformPanel;
	private JTextArea jSentencesTextArea;
	private JPanel jPanel3;
	private JTabbedPane jTabbedPane1;
	private JButton jStartButton;
	private JRadioButton jRadioButton2;
	private JRadioButton jRadioButton1;
	private JLabel jLabel1;
	private JPanel jPanel2;
	private JEditorPane jEditorPane1;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				KnowledgeCrawlerClient inst = new KnowledgeCrawlerClient();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public KnowledgeCrawlerClient() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Knowledge Crawler Client");
			{
				jPanel1 = new JPanel();
				BorderLayout jPanel1Layout = new BorderLayout();
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				{
					jTabbedPane1 = new JTabbedPane();
					jPanel1.add(jTabbedPane1, BorderLayout.CENTER);
					{
						jScrollPane1 = new JScrollPane();
						jTabbedPane1.addTab("Article", null, jScrollPane1, null);
						{
							jEditorPane1 = new JEditorPane();
							jScrollPane1.setViewportView(jEditorPane1);
						}
					}
					{
						jPanel3 = new JPanel();
						BorderLayout jPanel3Layout = new BorderLayout();
						jPanel3.setLayout(jPanel3Layout);
						jTabbedPane1.addTab("Sentences", null, jPanel3, null);
						{
							jScrollPane2 = new JScrollPane();
							jPanel3.add(jScrollPane2, BorderLayout.CENTER);
							{
								jSentencesTextArea = new JTextArea();
								jScrollPane2.setViewportView(jSentencesTextArea);
							}
						}
					}
					{
						jGeniformPanel = new JPanel();
						GroupLayout jGeniformPanelLayout = new GroupLayout((JComponent) jGeniformPanel);
						jGeniformPanel.setLayout(jGeniformPanelLayout);
						jTabbedPane1.addTab("Geniform", null, jGeniformPanel, null);
						{
							jTextField1 = new JTextField("This is a simple english sentence");
						}
						{
							jTransformButton = new JButton();
							jTransformButton.setText("Transform");
							jTransformButton.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									jTransformButtonActionPerformed(evt);
								}
							});
						}
						{
							jTabbedPane2 = new JTabbedPane();
							{
								jScrollPane3 = new JScrollPane();
								jTabbedPane2.addTab("Text", null, jScrollPane3, null);
								{
									jEditorPane2 = new JEditorPane();
									jScrollPane3.setViewportView(jEditorPane2);
									jEditorPane2.setText("");
								}
							}
							{
								jScrollPane4 = new JScrollPane();
								jTabbedPane2.addTab("Tree", null, jScrollPane4, null);
								{
									jTree1 = new JTree(new MyTreeModel(new StandfordNode(null, null)));
									jTree1.setCellRenderer(new MyTreeRenderer());
									jScrollPane4.setViewportView(jTree1);
								}
							}
							{
								jScrollPane5 = new JScrollPane();
								jTabbedPane2.addTab("Geniform", null, jScrollPane5, null);
								{
									jGeniformTree = new JTree();
									jScrollPane5.setViewportView(jGeniformTree);
								}
							}
						}
						{
							jLabel2 = new JLabel();
							jLabel2.setText("Please input a english sentense here");
						}
						jGeniformPanelLayout.setVerticalGroup(jGeniformPanelLayout.createSequentialGroup().addContainerGap(12, 12).addComponent(jLabel2,
								GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(
								jGeniformPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jTransformButton, GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE).addComponent(jTextField1, GroupLayout.Alignment.BASELINE,
										GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jTabbedPane2, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE).addContainerGap(12, 12));
						jGeniformPanelLayout.setHorizontalGroup(jGeniformPanelLayout.createSequentialGroup().addContainerGap(12, 12).addGroup(
								jGeniformPanelLayout.createParallelGroup().addGroup(
										jGeniformPanelLayout.createSequentialGroup().addGroup(
												jGeniformPanelLayout.createParallelGroup().addComponent(jTextField1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE,
														406, GroupLayout.PREFERRED_SIZE).addComponent(jLabel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 311,
														GroupLayout.PREFERRED_SIZE)).addGap(18).addComponent(jTransformButton, GroupLayout.PREFERRED_SIZE, 103,
												GroupLayout.PREFERRED_SIZE)).addComponent(jTabbedPane2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 761,
										GroupLayout.PREFERRED_SIZE)).addContainerGap(12, 12));
					}
				}
			}
			{
				jPanel2 = new JPanel();
				getContentPane().add(jPanel2, BorderLayout.NORTH);
				{
					jLabel1 = new JLabel();
					jPanel2.add(jLabel1);
					jLabel1.setText("Crawl from");
				}
				{
					jRadioButton1 = new JRadioButton();
					jPanel2.add(jRadioButton1);
					jRadioButton1.setText("google");
				}
				{
					jRadioButton2 = new JRadioButton();
					jPanel2.add(jRadioButton2);
					jRadioButton2.setText("Hong Kong google");
				}
				{
					jStartButton = new JButton();
					jPanel2.add(jStartButton);
					jStartButton.setText("Start");
					jStartButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jStartButtonActionPerformed(evt);
						}
					});
				}
			}
			this.setPreferredSize(new Dimension(800, 600));
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jStartButtonActionPerformed(ActionEvent evt) {
		String article = jEditorPane1.getText();
		article = article.replaceAll(System.getProperty("line.separator"), " ");
		String sentences[] = article.split("\\.");
		List<String> list = new ArrayList<String>(Arrays.asList(sentences));
		list.removeAll(Arrays.asList(""));
		sentences = list.toArray(sentences);
		this.jSentencesTextArea.setText("");
		for (int x = 0; x < sentences.length; x++) {
			this.jSentencesTextArea.setText(jSentencesTextArea.getText() + System.getProperty("line.separator") + sentences[x]);
		}
	}

	private void jTransformButtonActionPerformed(ActionEvent evt) {
		LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
		lp.setOptionFlags(new String[] { "-maxLength", "80", "-retainTmpSubcategories" });

		String[] sent = jTextField1.getText().split(" +");
		Tree parse = (Tree) lp.apply(Arrays.asList(sent));

		List<Tree> nodes = parse.getChildrenAsList();
		StandfordNode node = dumpTree(null, parse.firstChild());
		// String space = "";
		// for (Tree t : nodes) {
		// dumpTree(t, space, node, null);
		// }

		((MyTreeModel) jTree1.getModel()).setRoot(node);

		Geniform geniform = new Geniform();
		parseGeniform(geniform, node);

		// parse.pennPrint();
		// System.out.println();
		//
		// TreebankLanguagePack tlp = new PennTreebankLanguagePack();
		// GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
		// GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
		// Collection tdl = gs.typedDependenciesCollapsed();
		// jEditorPane2.setText(tdl.toString());

		StringWriter os = new StringWriter();
		PrintWriter pw = new PrintWriter(os, true);
		TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
		tp.printTree(parse, pw);
		jEditorPane2.setText(os.toString());
	}

	private void parseGeniform(Geniform geniform, StandfordNode node) {
		if (node != null) {
			ArrayList<StandfordNode> nodes = node.getChild();
			for (StandfordNode t : nodes) {
				System.out.println("t=" + t);
				if (t.equals("NP")) {
					geniform.NP.add(t);
				} else if (t.equals("VP")) {
					geniform.VP.add(t);
				} else if (t.equals("PP")) {
					geniform.PP.add(t);
				} else if (t.equals("A")) {
					geniform.A.add(t);
				} else if (t.equals("AP")) {
					geniform.A_PLUS.add(t);
				} else if (t.equals("D")) {
					geniform.D.add(t);
				} else if (t.equals("N")) {
					geniform.N.add(t);
				} else if (t.equals("NAME")) {
					geniform.NAME.add(t);
				}
				parseGeniform(geniform, t);
			}
		}
	}

	private StandfordNode dumpTree(StandfordNode parent, Tree node) {
		if (node != null) {
			StandfordNode createdNode = new StandfordNode(null, node);
			List<Tree> nodes = node.getChildrenAsList();
			for (Tree t : nodes) {
				StandfordNode childNode = dumpTree(createdNode, t);
				createdNode.getChild().add(childNode);
			}
			return createdNode;
		} else {
			return null;
		}
	}
}
